package com.sanxiau.dormitory.liu.controller;

import com.sanxiau.dormitory.liu.dao.*;
import com.sanxiau.dormitory.liu.entity.House;
import com.sanxiau.dormitory.liu.entity.Riches;
import com.sanxiau.dormitory.liu.entity.Student;
import com.sanxiau.dormitory.liu.entity.Visitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 来访人员控制层
 */
@Controller
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    HouseDao houseDao;

    @Autowired
    UserDao userDao;

    @Autowired
    DormDao dormDao;

    @Autowired
    RichesDao richesDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    VisitorDao visitorDao;

    //根据寝室楼查看来访信息
    @RequestMapping("/goVisitor")
    public String allVisitor(Model model,HttpSession session) {
        try {
            //得到登录账户
            String userName = session.getAttribute("userName").toString();
            //得到寝室楼号
            String houId = houseDao.findByUserName(userName).get(0).getHouId();
            List<Visitor> visitors = visitorDao.findByHouId(houId);
            model.addAttribute("visitors",visitors);
        } catch (Exception e) {
            //错误处理
            return "liu/error";
        }
        return "liu/visitor";
    }



    //查看所有来访信息
    @RequestMapping("/goAllVisitor")
    public String goAllVisitor(Model model,HttpSession session) {
        try {
            model.addAttribute("allVisitors", visitorDao.findAll());
        } catch (Exception e) {
            //错误处理
            return "liu/error";
        }
        return "liu/allVisitor";
    }


    //去增加页面
    @RequestMapping("/goAddVisitor")
    public String goAddVisitor(Model model) {
        return "liu/addVisitor";
    }


    //增加来访人员
    @PostMapping("/addVisitor")
    public String addVisitor(Visitor visitor, HttpSession session) {
        try {
            //值班人
            //得到登录账户
            String userName = session.getAttribute("userName").toString();
            String trueName = userDao.findUserByUserName(userName).get(0).getTrueName();
            visitor.setVisWatch(trueName);
            //楼房号
            String houId = houseDao.findByUserName(userName).get(0).getHouId();
            visitor.setHouId(houId);
            //来访日期
            //得到当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = simpleDateFormat.format(new Date());
            visitor.setVisDate(date);
            visitorDao.save(visitor);
                return "liu/success";
        } catch (Exception e) {
            return "liu/error";
        }

    }



}
